package com.fire.user.service.impl;

import com.fire.user.constants.AliConstants;
import com.fire.user.constants.ErrorCodeConstants;
import com.fire.user.constants.JkConstants;
import com.fire.user.util.AliyunSmsUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.fire.core.jkmodel.ReqDTO;
import com.fire.core.jkmodel.SmsInfoDTO;
import com.fire.core.jkmodel.XmlMessageDTO;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.GenratorNoHelper;
import com.fire.core.util.XmlMessageUtil;

@Service
public class SmsCodeService {
	private static Logger logger = Logger.getLogger(SmsCodeService.class);

	@Autowired
	private AliyunSmsUtil aliUtil;
	/*
	@Value("${smsCode.sendType}")
	private String sendType;  //1:jk短信验证码,2:阿里大于短信验证码*/
	
	public AjaxResult sendSmsCode(String mobileNo,String smsCode,int expireTimeInMinute) throws Exception {
			return AliSmsCode(mobileNo,smsCode);
	}
	
	public AjaxResult AliSmsCode(String mobileNo,String smsCode) throws Exception {
		logger.info("调用阿里短信验证码接口:mobileNo="+mobileNo+",smsCode="+smsCode);
		AjaxResult result = new AjaxResult();
		try {
			//读取数据库阿里云相关配置信息
			aliUtil.getConfigure();
			//短信內容变量替换格式待定
			String paramJsonStr = "{\"name\":\"AndyLau\", \"code\":\""+smsCode+"\"}";
			SendSmsResponse aliResponse = aliUtil.sendSms(mobileNo, paramJsonStr);
			if(aliResponse.getCode() != null && 
	            	AliConstants.ALI_SMS_REQ_SUCCESS.equals(aliResponse.getCode())) {
				result.setMessage("验证码发送成功");
	   		 	result.setTips("验证码发送成功");
				result.setStatus(ErrorCodeConstants.SUCCESS);
	        }else {
	        	result.setMessage(aliResponse.getMessage());
				result.setStatus(ErrorCodeConstants.ALI_INTERFACE_FAIL);
	        }
		}catch(Exception e) {
			logger.error("阿里短信验证码发送失败.",e);
			result.setMessage("阿里短信验证码发送失败");
			result.setStatus(ErrorCodeConstants.ALI_INTERFACE_FAIL);
		}
		return result;
	}

    /**
     * 构造短信验证码请求结构
     * 
     * @return
     */
    private SmsInfoDTO getSmsInfoDTO(String mobileNo,String smsCode,int expireTimeInMinute) {
        SmsInfoDTO smsInfo = new SmsInfoDTO();
        smsInfo.setMobileNo(mobileNo);
        smsInfo.setSmsCode(smsCode);
        smsInfo.setExpireTimeInMinute(expireTimeInMinute);
        return smsInfo;
    }
}
